# -*- coding: utf-8 -*-

import unittest
import time
import sys
sys.path.append('./')


from rpc.Server import Server
from rpc.Client import Client


class RPCTest(object):

    def __init__(self, value='RPCTest'):
        self._value = value

    def say_hello(self):
        print('say_hello called')
        return 'hello world'

    def my_add(self, val):
        print('my_add called')
        return self._value + '  ' + val


class TestNetWork():

    @staticmethod
    def start_server():
        s = Server(RPCTest, 'TestNetwork')
        s.setup(ip='localhost', port=3200)
        s.run()
        s.close()

    @staticmethod
    def callback_test():
        print('callback called')

    @staticmethod
    def start_client():
        c = Client(RPCTest)
        c.connect(ip='localhost', port=3200)
        print(c.say_hello(callback=TestNetWork.callback_test))
        for i in range(10):
            print(c.my_add('ha' * i))
        # 过早关闭socket会导致数据接收不完整
        time.sleep(1)
        c.close()
        # time.sleep(1)


if __name__ == '__main__':
    t = TestNetWork()
    t.start_client()
    # t.start_server()
